package com.lixl.thread;

import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Ttest04 {
	public static void main(String[] args) {

		User u1 = new User("张三");
		User u2 = new User("李四");
		u1.start();
		u2.start();

	}

}

class Book {
	public static int bookPage = 100;
	public static int readDays = 1;

	public static Lock lock = new ReentrantLock();
	
	public synchronized static int read(int pageSize){
		if(pageSize<1) return 0;
		int k = (int) (new Random().nextDouble() * 20);
		if (k < 1) return 0;
		
		bookPage = pageSize-k;
		
		return k;
	}

}

class User extends Thread {
	public User(String name) {
		this.setName(name);
	}

	@Override
	public void run() {
		synchronized (this) {
			int k = Book.read(Book.bookPage);
			System.out.println(this.getName()+"\t"+k+"\t"+Book.bookPage);
		}
	}

}
